package com.house.api.controller;

import com.house.common.context.BaseContext;
import com.house.common.context.RedisContext;
import com.house.common.result.HttpCodeEnum;
import com.house.common.result.Result;
import com.house.model.dto.MessageDto;
import com.house.model.dto.MessageListVo;
import com.house.model.vo.MessageContactVO;
import com.house.model.vo.MessageVO;
import com.house.service.service.IInformationRecordMapperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Api(value = "信息接口",tags = "信息接口")
public class InformationRecordController {

    @Autowired
    private IInformationRecordMapperService informationRecordMapperService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/contact")
    @ApiOperation("获取联系人")
    public Result getContact() {
        List<MessageContactVO> contact = informationRecordMapperService.findContact();
        return Result.ok(contact);
    }

    @GetMapping("/{userId}")
    @ApiOperation("获取当联系人的所有信息")
    public Result getMessage(@PathVariable("userId") String userId) {
        List<MessageListVo> messageList = informationRecordMapperService.getMessageList(userId);
        return Result.ok(messageList);
    }

    @PostMapping("/sendMessage")
    @ApiOperation("发送信息")
    public Result sendMessage(@RequestBody MessageDto messageDto) {
        MessageListVo messageListVo = informationRecordMapperService.sendMessage(messageDto);
        return Result.ok(messageListVo);
    }
    @GetMapping("/getRedisMessage/{userId}")
    @ApiOperation("获取当联系人的所有信息")
    public Result getRedisMessage(@PathVariable("userId") String userId) {
        String myUserId = BaseContext.getCurrentId();
        Object o = redisTemplate.opsForValue().get(RedisContext.MESSAGE + userId + ":" + myUserId);
        if (o == null) {
            return Result.error(HttpCodeEnum.MESSAGE_NOT);
        }else {
            redisTemplate.delete(RedisContext.MESSAGE + userId + ":" + myUserId);
            List<MessageListVo> messageList = informationRecordMapperService.getMessageList(userId);
            return Result.ok(messageList);
        }
    }

}

